package uk.co.richardwkirk.parkrun.data;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class ParkrunDataExpiryCheck {

	Date loadTime;
	Date currentTime;
	
	public ParkrunDataExpiryCheck(String time) throws ParseException {
		// When was the data loaded?
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		loadTime = df.parse(time);
		
		// Current time
		currentTime = Calendar.getInstance().getTime();
	}
	
	public void setCurrentTime(String time) throws ParseException {
		// Change the current time
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		currentTime = df.parse(time);
	}

	public boolean check() {
		Date lastSaturday9am = currentTime;
		lastSaturday9am.setHours(9);
		lastSaturday9am.setMinutes(0);
		lastSaturday9am.setSeconds(0);
		Calendar cal = Calendar.getInstance();
		cal.setTime(lastSaturday9am);
		while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY)
		{
			cal.add(Calendar.DATE, -1);
		}
		lastSaturday9am = cal.getTime();
		
		return (loadTime.before(lastSaturday9am));
	}
	
}
